<?php
/**
 * Yotaku Shop - yshop
 * @author Creatint<creatint@163.com>
 * @copyright www.yotaku.cn 2016
 */
namespace Admin\Model;
use Think\Model;
use Think\Page;
class UserModel extends Model{
    protected $error   =   '';
    protected $show   =   '';
    //自动验证
    protected $_validate = array(
        /* 验证name */
        array('username', '', '用户名已存在', self::MUST_VALIDATE,'unique'),
        array('username', '1,20', '用户名长度为1-20个字符', self::MUST_VALIDATE,'length'),
        array('username', 'require', '用户名不能为空', self::MUST_VALIDATE),

        /* 验证email */
        array('email', 'require', 'Email不能为空', self::MUST_VALIDATE),
        array('email', 'email', 'Email格式不正确', self::MUST_VALIDATE),

        /* 验证mobile */
        array('mobile', '/^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|14[57])[0-9]{8}$/', '手机号格式不正确', self::EXISTS_VALIDATE),

        //验证password
        array('password','3,32','密码长度为3-32位字符',self::EXISTS_VALIDATE,'length'),

    );
    //自动完成
    protected $_auto = array(
        array('status', 1, self::MODEL_INSERT),
        array('password','md5',self::MODEL_BOTH,'function')
    );


    public function getUsers($params=null,$page=false){
        //初始化查询条件
        $map    =   array();
        if($page){
            $result =   $this->where($map)->page($_GET['p'].',25')->select();
        }else{
            $result =   $this->where($map)->select();
        }
        if($result && $page){
            $count  =   $this->where($map)->count();
            trace($count,'count');
            $Page   =   new Page($count,25);
            $Page->setConfig('wrap',array('<li>','</li>','<li class="active">'));
            $Page->setConfig('prev','«');
            $Page->setConfig('next','»');
            $this->show   =   $Page->show();
        }
        return $result;
    }
    public function getDetail($id,$field=true){
        $info   =   $this->field($field)->find($id);
        if(!is_array($info)){
            $this->error    =   '用户不存在';
            return false;
        }
        return $info;
    }

    public function update($data=null){
        if(!$data){
            $this->error = '数据为空';
            return false;
        }
        $data = $this->create($data);
        dump($data);
        dump($this->getError());
        exit;
        if($data['id']){
            $this->where("id={$data['id']}")->save($data);
        }else{
            $id = $this->add();
            if(!$id){
                $this->error = '新增失败';
                return false;
            }
        }
        return $data;
    }
    public function del($id){
        if(empty($id)){
            $this->error = '选择要删除的用户';
        }
        $map = array('id'=>array('IN',trim(implode(',',$id),',')));
        if($this->where($map)->delete()){
            return true;
        }else{
            $this->error = '无此条数据';
            return false;
        }
    }
    public function getError(){
        return $this->error;
    }
    public function getShow(){
        return $this->show;
    }
}